<?php
/*
* YAPP utils functions file
*/

function print_r_f($arr)
{
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}

function secondsToTime($value)
{
    $hours     = floor($value/(60*60));
    $remaining = $value - ($hours*60*60);
    $minutes   = floor($remaining/60);
    $remaining     = $remaining - ($minutes*60);
    $hours   = ($hours > 9)?$hours:'0'.$hours;
    $minutes = ($minutes > 9)?$minutes:'0'.$minutes;
    $seconds = ($remaining > 9)?$remaining:'0'.$remaining;
    return $hours.':'.$minutes.':'.$seconds;
}

function validEmail($email)
{
    if(filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        return true;
    }
    else
    {
        return false;
    }
}

function __autoload($className)
{
    // Check if it's a model, a controller, or a view
    $endsWith  = false;
    $modulePos = false;
    if (($modLength = strpos($className, 'Model')) > -1)
    {
        $endsWith  = 'Model';
        $modulePos = $modLength;
    }
    else if (($viewLength = strpos($className, 'View')) > -1)
    {
        $endsWith  = 'View';
        $modulePos = $viewLength;
    }
    else if (($ctrlLength = strpos($className, 'Controller')) > -1)
    {
        $endsWith  = 'Controller';
        $modulePos = $ctrlLength;
    }
    if (!$endsWith)
    {
        die("COULDN'T LOCATE CLASS $className!!!");
    }
    else
    {
        $moduleName = strtolower(substr($className,0,$modulePos));
        $file = BASE.'modules/'.$moduleName.'/'.$className.'.php';
        if (file_exists($file))
        {
            require_once($file);
            return true;
        }
        else
        {
            die("COULDN'T LOCATE CLASS $className (autoload search on $file)!!!");
        }
    }
}

?>